package com.nondo.dean.auth.provider.impl;


import com.nondo.dean.auth.common.bean.AppItem;
import com.nondo.dean.auth.common.bean.AuthorityItem;
import com.nondo.dean.auth.common.bean.MenuItem;
import com.nondo.dean.auth.common.bean.RoleItem;
import com.nondo.dean.auth.common.BusinessUtils;
import com.nondo.dean.auth.provider.AppManager;
import com.nondo.dean.auth.provider.AuthorityManager;
import com.nondo.dean.auth.provider.MenuManager;
import com.nondo.dean.auth.provider.RoleManager;
import com.nondo.dean.auth.service.AppService;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.common.web.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.nondo.dean.auth.common.BusinessUtils.OPERA_REMOVE;

@Service
public class AppManagerImpl implements AppManager {

    @Autowired
    private AppService appService;
    @Autowired
    private AuthorityManager authorityManager;
    @Autowired
    private RoleManager roleManager;
    @Autowired
    private MenuManager menuManager;

    @Override
    public View<String> requestAppRegister(AppItem appItem) {


        String status = appService.save(BusinessUtils.toApp(appItem));
        if ("new".equals(status)) {
            appItem.setNotUpdate(0);
        }

        if (appItem.getNotUpdate() != 1) {

            // 初始化角色
            if (!appItem.getRoles().isEmpty()) {
                for (Map.Entry<String, List<RoleItem>> entry : appItem.getRoles().entrySet()) {
                    if (OPERA_REMOVE.equals(entry.getKey())) {
                        for (RoleItem roleItem : entry.getValue()) {
                            if (roleItem != null && roleItem.getId() != null) {
                                roleManager.deleteRole(roleItem.getId());
                            }
                        }
                    } else {
                        for (RoleItem roleItem : entry.getValue()) {
                            if (roleItem != null && roleItem.getId() != null) {
                                roleManager.registerRole(roleItem);
                            }
                        }
                    }
                }
            }


            // 初始化权限
            if (!appItem.getAuthorities().isEmpty()) {
                for (Map.Entry<String, List<AuthorityItem>> entry : appItem.getAuthorities().entrySet()) {
                    if (OPERA_REMOVE.equals(entry.getKey())) {
                        for (AuthorityItem authorityItem : entry.getValue()) {
                            if (authorityItem != null && authorityItem.getId() != null) {
                                authorityManager.deleteAuthority(authorityItem.getId());
                            }
                        }
                    } else {
                        for (AuthorityItem authorityItem : entry.getValue()) {
                            if (authorityItem != null && authorityItem.getId() != null) {
                                authorityManager.registerAuthority(authorityItem);
                            }
                        }
                    }
                }
            }


            // 初始化角色
            if (!appItem.getMenus().isEmpty()) {
                for (Map.Entry<String, List<MenuItem>> entry : appItem.getMenus().entrySet()) {
                    if (OPERA_REMOVE.equals(entry.getKey())) {
                        for (MenuItem menuItem : entry.getValue()) {
                            if (menuItem != null && menuItem.getId() != null) {
                                menuManager.requestMenuDelete(menuItem.getId());
                            }
                        }
                    } else {
                        for (MenuItem menuItem : entry.getValue()) {
                            if (menuItem != null && menuItem.getId() != null) {
                                menuManager.requestMenuRegister(menuItem);
                            }
                        }
                    }
                }
            }


        }


        return View.ofOk("应用注册成功", "0");
    }
}
